package viewPackage.menus;

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;

import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

@SuppressWarnings("serial")
public class ResolutionSlider extends JSlider {

	private boolean Bsvga = false;
	private boolean Bxga = false;
	private boolean Bhd720 = false;
	private boolean Bwxga = false;
	private boolean Bwxga2 = false;
	private boolean Bsxga = false;
	private boolean Bwsxga = false;
	private boolean Bwwsxga = false;
	
	public ResolutionSlider(int startValue) {
		super(0, 7, startValue);
		
		super.setPaintTicks(true);
		super.setSnapToTicks(true);
		super.setBackground(Color.BLACK);
		super.setForeground(Color.YELLOW);
		
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice device = ge.getDefaultScreenDevice();
		DisplayMode[] modes = device.getDisplayModes();
			
		if (modes.length != 0) {
			for (int i = 0; i < modes.length; i++) {
				if (modes[i].getWidth() == 800 && modes[i].getHeight() == 600 && modes[i].getBitDepth() == 32) {
					Bsvga = true;
				}
				if (modes[i].getWidth() == 1024 && modes[i].getHeight() == 768 && modes[i].getBitDepth() == 32) {
					Bxga = true;
				}
				if (modes[i].getWidth() == 1280 && modes[i].getHeight() == 720 && modes[i].getBitDepth() == 32) {
					Bhd720 = true;
				}
				if (modes[i].getWidth() == 1280 && modes[i].getHeight() == 768 && modes[i].getBitDepth() == 32) {
					Bwxga = true;
				}
				if (modes[i].getWidth() == 1280 && modes[i].getHeight() == 800 && modes[i].getBitDepth() == 32) {
					Bwxga2 = true;
				}
				if (modes[i].getWidth() == 1280 && modes[i].getHeight() == 1024 && modes[i].getBitDepth() == 32) {
					Bsxga = true;
				}
				if (modes[i].getWidth() == 1440 && modes[i].getHeight() == 900 && modes[i].getBitDepth() == 32) {
					Bwsxga = true;
				}
				if (modes[i].getWidth() == 1600 && modes[i].getHeight() == 900 && modes[i].getBitDepth() == 32) {
					Bwwsxga = true;
				}
			}
		}
		
		this.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider slider = (JSlider) e.getSource();
				//Move up is true.  Move down is false.
				boolean direction = true;
				while (!validResolution(slider.getValue())) {
					if (slider.getValue() == slider.getMaximum()) {
						direction = false;
					}
					else if (slider.getValue() == slider.getMinimum()) {
						direction = true;
					}
					int newValue = 0;
					if (direction == true) {
						newValue = slider.getValue() + 1;
					}
					else if (direction == false) {
						newValue = slider.getValue() - 1;
					}
					slider.setValue(newValue);
				}
			}			
		});
		
	}
	
	private boolean validResolution(int r) {
		if (r == 0 && Bsvga == true) {
			return true;
		}
		else if (r == 1 && Bxga == true) {
			return true;
		}
		else if (r == 2 && Bhd720 == true) {
			return true;
		}
		else if (r == 3 && Bwxga == true) {
			return true;
		}
		else if (r == 4 && Bwxga2 == true) {
			return true;
		}
		else if (r == 5 && Bsxga == true) {
			return true;
		}
		else if (r == 6 && Bwsxga == true) {
			return true;
		}
		else if (r == 7 && Bwwsxga == true) {
			return true;
		}
		else {
			Toolkit.getDefaultToolkit().beep();
			return false;
		}
	}
	
}
